package util;


import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MhtToHtmlUtil {


    public static void main(String[] args) throws IOException {
        mht2html("E:\\mht文件转换\\会议\\会审会议纪要2.mht", "E:\\mht文件转换\\会议\\会审会议纪要2.htm");
    }
    public static void mht2html(String s_SrcMht, String s_DescHtml) {
        try {
            InputStream fis = new FileInputStream(s_SrcMht);
//            String strText = IOUtils.toString(fis,"utf-8");
            String strText = IOUtils.toString(fis);

            // 获取mht文件内容代码的编码
//            String strEncodng = "UTF-8";

//            int a1 = strText.indexOf("<html>");
//            int a2 = strText.indexOf("</html>");
//            strText = strText.substring(a1, a2 + 7);

            strText = decodeQuotedPrintable(strText, "gb2312");
            if (strText == null)
                return;


//            List titleStrs = new ArrayList();
//            String projectName = "";
            Document doc = Jsoup.parse(strText);
//
//            Elements elements = doc.getElementsByAttributeValue("itemcd","PROJECTNAME");
//            if (elements!= null && elements.size()!=0){
//                projectName = elements.get(0).text();
//            }
//
//            System.out.println(projectName);

            //正文内容
            StringBuilder text = new StringBuilder();
            Elements texts = doc.getElementsByAttributeValue("class","MsoNormal");
            for (Element element:texts
                 ) {
                text.append(element.text());
            }

            System.out.println(text.toString());
            //时间
//            String timeStr = doc.getElementById("opinionID").getElementsByTag("table").get(0).getElementsByTag("td").get(1).getElementsByTag("center").text();
//            timeStr = timeStr.substring(timeStr.indexOf(" "),timeStr.indexOf(" ")+11);
//            System.out.println("立项批复时间："+timeStr);
            //标题
//            Elements titleEles = doc.getElementsByAttributeValue("itemcd","APPLYTITLE");
//            for(Element element:titleEles) {
//                String titlestr = element.text();
//                System.out.println("标题："+titlestr);
//                titleStrs.add(titlestr);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 将提取出来的html内容写入保存的路径中。
     *
     * @param s_HtmlTxt
     * @param s_HtmlPath
     * @param s_Encode
     */
    public static boolean SaveHtml(String s_HtmlTxt, String s_HtmlPath,
                                   String s_Encode) {
        try {
            Writer out = null;
            out = new OutputStreamWriter(
                    new FileOutputStream(s_HtmlPath, false), s_Encode);
            out.write(s_HtmlTxt);
            out.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static String decodeQuotedPrintable(String str, String encoding) {
        if  (str ==  null ) {
            return  null ;
        }
        try  {
            //str = str.replaceAll("=\n", "");//??
            byte [] bytes = str.getBytes( "US-ASCII" );

            ByteArrayOutputStream buffer =  new  ByteArrayOutputStream();
            for  ( int  i =  0 ; i < bytes.length; i++) {
                int  b = bytes[i];
                if  (b ==  '=' ) {
                    int  u = Character.digit(( char ) bytes[++i],  16 );
                    int  l = Character.digit(( char ) bytes[++i],  16 );
                    if  (u == - 1  || l == - 1 ) { //??
                        continue ;
                    }
                    buffer.write(( char ) ((u <<  4 ) + l));
                }  else  {
                    buffer.write(b);
                }
            }
            return  buffer.toString(encoding);
        }
        catch  (Exception e) {
            e.printStackTrace();
            return  str;
        }
    }
}
